/**
 * 专门为login服务的reducer
 */
import { getToken, removeToken, setToken } from '@/utils/auth'
import { LOYOUT, SAVE_Token, SAVE_USER_INFO } from '../actionType'
const initialState = {
  token: getToken() || '',
  userInfo: ''
}

export function loginReducer (state = initialState, action) {
  const { type, data } = action
  switch (type) {
    case SAVE_Token:
      setToken(data)
      return {
        ...state,
        token: data
      }
    case SAVE_USER_INFO:
      return {
        ...state,
        userInfo: data
      }
    case LOYOUT:
      // 移除token
      removeToken()
      // 重置state数据
      return {
        token : '',
        userInfo: ''
      }
    default:
      return state
  }

}